<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <treeTools :node-data="com" :is-root="true" @add="addDepartments" />
        <hr>
        <el-tree :data="depts" :default-expand-all="true" :expand-on-click-node="false">
          <template #default="obj">
            <treeTools :node-data="obj.data" @del="delDepartments" @add="addDepartments" @edit="editDepartments" />
          </template>
        </el-tree>
        <addDept
          ref="edit"
          :is-show-dialog.sync="isShowDialog"
          :node-data="nodeData"
          @add="getDepartments"
        />
      </el-card>
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import addDept from './components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import { listDepartments } from '@/utils/index'
export default {
  components: { treeTools, addDept },
  data() {
    return {
      com: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      depts: [],
      isShowDialog: false,
      nodeData: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const { depts } = await getDepartments()
      // // console.log(depts)
      // this.depts = depts
      this.depts = listDepartments(depts, '')
    },
    delDepartments() {
      this.getDepartments()
    },
    addDepartments(nodeData) {
      this.isShowDialog = true
      this.nodeData = nodeData
    },
    editDepartments(nodeData) {
      this.$refs.edit.editDepartment(nodeData.id)
      this.isShowDialog = true
      this.nodeData = nodeData
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
